
import xp3
import nsa

import re
import sys

from archive_base import Entry

class XP3Entry(Entry):
	def __init__(self, entry, path):
		self.name = entry.name
		self.size = entry.original_size
		self.xp3_entry = entry
		self.path = path
	def decode(self):
		return xp3.read_data(self.path, self.xp3_entry, xp3.fate_decrypter)


class XP3Reader:
	def __init__(self, path):
		self.path = path
		self.entries = [XP3Entry(i, path) for i in xp3.read_xp3(path)]

try:
	from xp3_c import XP3Entry, XP3Reader
except:
	pass
try:
	from nsa_c import NSAEntry, NSAReader
except:
	pass

class NSAReader:
	def __init__(self, path):
		self.path = path
		self.entries = nsa.read_indexes(path)
		for entry in self.entries:
			entry.path = path

def Entries(path):
	if re.search(r'\.xp3$', path, re.I):
		return XP3Reader(path).entries
	elif re.search(r'\.nsa$', path, re.I):
		return NSAReader(path).entries
	else:
		raise Exception("What's "+path+"?")

